package com.jbookstore.dao.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.jbookstore.dao.BookDao;
import com.jbookstore.dto.BookDto;

public class BookDaoImpl implements BookDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<BookDto> selectBookList(String categoryId) {

		StringBuilder querySb = new StringBuilder();
		querySb.append("SELECT * FROM BOOK");
		if (categoryId != "") {
			querySb.append(" WHERE category_id = '").append(categoryId)
					.append("'");
		}
		querySb.append(";");
		System.out.println(querySb.toString());

		List<BookDto> bookList = this.jdbcTemplate.query(querySb.toString(),
				new RowMapper<BookDto>() {
					public BookDto mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						BookDto book = new BookDto();
						book.setBookId(rs.getString("book_id"));
						book.setCategory_id("category_id");
						book.setTitle(rs.getString("title"));
						book.setAuthor(rs.getString("author"));
						book.setPublisher(rs.getString("publisher"));
						NumberFormat nf = NumberFormat.getNumberInstance();;
						book.setPrice(nf.format(rs.getInt("price")) + " 원");
						book.setQty(rs.getInt("qty"));
						book.setOrder_qty(rs.getInt("order_qty"));
						book.setImg("images/book/" + rs.getString("book_id") + ".jpg");
						book.setCodeId(rs.getString("code_id"));
						book.setCodeGroupId(rs.getString("codegroup_id"));
						book.setSummary(rs.getString("summary"));
						
						return book;
					}
				});
		return bookList;

	}

	public BookDto selectBookDetail(String bookId) {

		StringBuilder sqlSb = new StringBuilder();

		sqlSb.append("SELECT book.*, code.code_nm FROM book ");
		sqlSb.append("INNER JOIN code ");
		sqlSb.append("ON book.code_id=code.code_id ");
		sqlSb.append("WHERE book_id='").append(bookId).append("'");
		
		RowMapper<BookDto> mapper = new RowMapper<BookDto>() {
			public BookDto mapRow(ResultSet rs, int rowNum) throws SQLException {
				BookDto book = new BookDto();
				book.setBookId(rs.getString("book_id"));
				book.setCategory_id("category_id");
				book.setTitle(rs.getString("title"));
				book.setAuthor(rs.getString("author"));
				book.setPublisher(rs.getString("publisher"));
				NumberFormat nf = NumberFormat.getNumberInstance();;
				book.setPrice(nf.format(rs.getInt("price")) + " 원");
				book.setQty(rs.getInt("qty"));
				book.setOrder_qty(rs.getInt("order_qty"));
				book.setImg("images/book/" + rs.getString("book_id") + ".jpg");
				book.setCodeId(rs.getString("code_id"));
				book.setCodeGroupId(rs.getString("codegroup_id"));
				book.setSummary(rs.getString("summary"));
				book.setCodeNm(rs.getString("code_nm"));

				return book;
			}
		};
		
		return (BookDto) jdbcTemplate.queryForObject(sqlSb.toString(),
				new Object[] {}, mapper);
	}

	public List<BookDto> selectBookListByIds(String bookIds) {

		StringBuilder querySb = new StringBuilder();
		querySb.append("SELECT * FROM BOOK");
		querySb.append(" WHERE book_id in (").append(bookIds).append(")");
		querySb.append(";");
		System.out.println(querySb.toString());

		List<BookDto> bookList = this.jdbcTemplate.query(querySb.toString(),
				new RowMapper<BookDto>() {
					public BookDto mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						BookDto book = new BookDto();
						book.setBookId(rs.getString("book_id"));
						book.setCategory_id("category_id");
						book.setTitle(rs.getString("title"));
						book.setAuthor(rs.getString("author"));
						book.setPublisher(rs.getString("publisher"));
						NumberFormat nf = NumberFormat.getNumberInstance();;
						book.setPrice(nf.format(rs.getInt("price")) + " 원");
						book.setQty(rs.getInt("qty"));
						book.setOrder_qty(rs.getInt("order_qty"));
						book.setImg("images/book/" + rs.getString("book_id") + ".jpg");
						book.setCodeId(rs.getString("code_id"));
						book.setCodeGroupId(rs.getString("codegroup_id"));
						book.setSummary(rs.getString("summary"));
						
						return book;
					}
				});
		return bookList;		
	}
}
